import { formatVoteStates, formatVoteCounts, formatFileSize, formatDisplayPath } from '@/utils/format';

export const formatFolder = folder => ({
  id: folder.id,
  type: 'ProductFolder',
  img: 'icon-folder',
  title: folder.title,
  path: `/spaces/${folder.product_id}/resources/${folder.id}`,

  displayPath: formatDisplayPath(`${folder.parent_path || ''}/${folder.id}-${folder.title}`), // 用于展示的路径

  space: {
    id: folder.product_id,
    title: folder.product_title,
  },

  created_at: folder.created_at,
  updated_at: folder.updated_at,
});

export const formatAttach = attach => ({
  type: 'ProductDocument',
  icon: '',

  id: attach.id,
  title: attach.title,

  key: attach.ufile_key,
  size: attach.ufile_size,
  sized: formatFileSize(attach.ufile_size),

  downloadCount: attach.count,

  author: attach.owner || {},

  /* element-ui 组件中一些其它的必要的参数 */
  status: 'success',
  name: attach.title,
  response: {
    key: attach.ufile_key,
    url: attach.ufile,
  },

  created_at: attach.created_at,
  updated_at: attach.updated_at,
});

export const formatAttaches = attaches => attaches.map(attach => formatAttach(attach));

const formatAnchorContent = (content) => {
  const element = document.createElement('div');
  const headerTags = ['h1', 'h2', 'h3', 'h4', 'h5', 'h6'];
  element.innerHTML = content;
  const headers = element.querySelectorAll(headerTags.join(','));
  for (let i = 0; i < headers.length; i += 1) {
    headers[i].setAttribute('data-doc-catalog-index', `data-doc-catalog-index-${i}`);
  }
  return element.innerHTML;
};

export const formatDoc = doc => ({
  type: 'ProductSnippet',
  img: 'icon-file',

  id: doc.id,
  title: doc.title,
  path: `/spaces/${doc.product_id}/resources-docs/${doc.id}`,
  parentPath: doc.parent_path,
  desc: doc.description,
  content: doc.content,
  anchorContent: formatAnchorContent(doc.content),
  tags: doc.tags || [],
  attaches: formatAttaches(doc.documents || []),

  author: doc.owner || {},

  core: doc.is_core,
  displayPath: formatDisplayPath(doc.parentPath || ''),
  good: doc.is_good,
  wxShare: doc.share_url, // 微信小程序二维码图片地址

  voteStates: formatVoteStates(doc.has_vote),
  voteCounts: formatVoteCounts(doc.votes),
  voteState: formatVoteStates(doc.has_vote), // 与issue兼容
  voteCount: formatVoteCounts(doc.votes), // 与issue兼容
  commentsCount: doc.comment_count,

  space: { // 所在空间
    id: doc.product_id,
    title: doc.product_title,
    path: `/spaces/${doc.product_id}`,
  },

  created_at: doc.created_at,
  updated_at: doc.updated_at,
});

export const formatUploadIcon = (ext) => {
  switch (ext) {
    case 'doc': case 'docx':
      return 'icon-w';
    case 'ppt': case 'pptx': case 'pps': case 'pot':
      return 'icon-p';
    case 'xls': case 'xlsx':
      return 'icon-x';
    case 'pdf':
      return 'icon-pdf';
    case 'txt':
      return 'icon-t';
    case 'vsd':
      return 'icon-vs';
    case 'epub':
      return 'icon-ep';
    case 'rtf':
      return 'icon-rtf';
    case 'wps': case 'et': case 'dps':
      return 'icon-wps';
    case 'xmind':
      return 'icon-xmind';
    case 'dwg':
      return 'icon-dwg';
    case 'jpg': case 'jpeg': case 'png': case 'svg': case 'gif': case 'bmp':
      return 'icon-image';
    default:
      return 'icon-unknown';
  }
};

export const formatUpload = upload => ({
  type: 'ProductLocalFile',
  ext: upload.ext.substr(upload.ext.indexOf('.') + 1),
  img: formatUploadIcon(upload.ext.substr(upload.ext.indexOf('.') + 1)),
  id: upload.id,
  title: upload.title,
  tags: upload.tags || [],
  author: upload.owner || {},
  path: `/spaces/${upload.product_id}/resources-uploads/${upload.id}`,
  parentPath: upload.parent_path,
  displayPath: formatDisplayPath(upload.parentPath || ''),
  good: upload.is_good,

  key: upload.ufile_key,
  wxShare: upload.share_url, // 微信小程序二维码图片地址

  voteStates: formatVoteStates(upload.has_vote),
  voteCounts: formatVoteCounts(upload.votes),
  voteState: formatVoteStates(upload.has_vote), // 与issue兼容
  voteCount: formatVoteCounts(upload.votes), // 与issue兼容
  commentsCount: upload.comment_count,

  space: { // 所在空间
    id: upload.product_id,
    title: upload.product_title,
    path: `/spaces/${upload.product_id}`,
  },

  created_at: upload.created_at,
  updated_at: upload.updated_at,
});

export const formatResource = (resource) => {
  switch (resource.type) {
    case 'ProductFolder': return formatFolder(resource); // 目录
    case 'ProductDocument': return formatAttach(resource); // 附件
    case 'ProductSnippet': return formatDoc(resource); // 文档
    case 'ProductLocalFile': return formatUpload(resource); // 上传的文件
    default: return { err: true };
  }
};

export const formatResources = resources => resources.map(resource => formatResource(resource));
